package com.xsjl.article.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xsjl.article.mapper.ArticleMapper;
import com.xsjl.article.pojo.Article;
import com.xsjl.article.pojo.ArticleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * article服务层
 * 
 * @author Administrator
 *
 */
@Service
public class ArticleService {

	

	@Autowired
	private ArticleMapper articleMapper;
	

	@Transactional
	public void examine(String id) {
		articleMapper.updateState(id);
	}

	@Transactional
	public void addThumbup(String id) {
		articleMapper.addThumbup(id);
	}
	/**
	 * 查询全部列表
	 * @return
	 */
	// @Cacheable(cacheNames = "articles")
	public List<ArticleVO> findAll() {
		return articleMapper.findAll();
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<Article> findSearch(Map whereMap, int page, int size) {
		Page<Article> pages = new Page<>(page, size);
		QueryWrapper<Article> wrapper = new QueryWrapper<>();
		wrapper.allEq(whereMap);

		Page<Article> articlePage = articleMapper.selectPage(pages, wrapper);
		return articlePage;
	}

	
	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<Article> findSearch(Map whereMap) {
		QueryWrapper<Article> wrapper = new QueryWrapper<>();
		wrapper.allEq(whereMap);
		return articleMapper.selectList(wrapper);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	public Article findById(String id) {
		return articleMapper.selectById(id);
	}

	/**
	 * 增加
	 * @param article
	 */
	// 如果缓存中有数据，那么清除缓存
	// @CacheEvict(cacheNames = "article", allEntries = true)
	public void add(Article article) {
		articleMapper.insert(article);
	}

	/**
	 * 修改
	 * @param article
	 */
	public void update(Article article) {
		articleMapper.updateById(article);
	}

	/**
	 * 删除
	 * @param id
	 */
	public void deleteById(String id) {
		articleMapper.deleteById(id);
	}

	

}
